package com.yuanss.backenddemo.config;

import io.lettuce.core.ClientOptions;
import io.lettuce.core.protocol.ProtocolVersion;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.data.redis.LettuceClientConfigurationBuilderCustomizer;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;

@Configuration
public class LettuceClientConfig implements LettuceClientConfigurationBuilderCustomizer {

    @Value("${spring.redis.lettuce.protocol-version}")
    public String protocolVersion;

    @Override
    public void customize(LettuceClientConfiguration.LettuceClientConfigurationBuilder clientConfigurationBuilder) {
        if ("RESP3".equals(protocolVersion)) {
            clientConfigurationBuilder.clientOptions(ClientOptions.builder()
                    .protocolVersion(ProtocolVersion.RESP3)
                    .build());
        } else {
            clientConfigurationBuilder.clientOptions(ClientOptions.builder()
                    .protocolVersion(ProtocolVersion.RESP2)
                    .build());
        }
    }
}